<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;

class VehicleSeries extends SortableModel
{
    use HasFactory;
    use Cachable;

    protected $fillable = [
        'series_name', 'recommend', 'clue_point', 'driver_point', 'buy_car_point', 'recommend_poster', 'poster'
    ];

    protected $casts = [
        'recommend' => 'boolean',
        'clue_point' => 'integer',
        'driver_point' => 'integer',
        'buy_car_point' => 'integer'
    ];

    public function vehicles()
    {
        return $this->hasMany(Vehicle::class)->orderBy('order', 'desc');
    }

    public function dealers()
    {
        return $this->belongsToMany(Dealer::class)->withTimestamps();
    }

    public function isDealer($dealer_id)
    {
        return $this->dealers->contains($dealer_id);
    }

    public function associatedShop($dealer_ids)
    {
        if (!is_array($dealer_ids)) {
            $dealer_ids = compact('dealer_ids');
        }
        $this->dealers()->sync($dealer_ids, false);
    }

    public function unAssociatedShop($dealer_ids)
    {
        if (!is_array($dealer_ids)) {
            $dealer_ids = compact('dealer_ids');
        }
        $this->dealers()->detach($dealer_ids);
    }
}
